package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerPersonInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @createTime: 2023-10-07
 * @creater: yong
 * 基础人员信息控制
 */
@RestController
@Slf4j
@RequestMapping("/v1/cerpersoninfo/")
public class CerPersonInfoController extends BaseController {
    @Autowired
    private CerPersonInfoService cerPersonInfoService;
    @GetMapping("nationlist")
    JsonObj listPageData(){
        return cerPersonInfoService.nationlist();
    }
    @GetMapping("listPagePerson")
    JsonObj listPagePerson(){
        return cerPersonInfoService.listPagePerson(getPageData());
    }
    @PostMapping("uploadFile4CerDataInit")
    void uploadFile4CerDataInit(@RequestParam("file") MultipartFile file, HttpServletResponse response){
        cerPersonInfoService.listPagePerson(getPageData());
    }
    @PostMapping("editPersonInfo")
    public JsonObj editPersonInfo(){
        return cerPersonInfoService.editPersonInfo(getPageData());
    }

    @PostMapping("queryPersonInfoByNameOrIdCard")
    public JsonObj queryPersonInfoByNameOrIdCard(){
        return cerPersonInfoService.queryPersonInfoByNameOrIdCard(getPageData());
    }

    @PostMapping("getCity")
    @ApiOperation("获取地区数据根据层级")
    public JsonObj getCity() {
        return cerPersonInfoService.getCity(getPageData());
    }

    @PostMapping("exportPersonInfo")
    @ApiOperation("导出初始数据")
    public void exportSysteminitImportInfo(HttpServletResponse response) {
        PageData pageData = getPageData();

        // 查询出可以用来导出的数据源
        JsonObj jsonObj = cerPersonInfoService.queryAllExportData(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
                .getData()
                .get("data");

        // 组装表头
        List<String> headList = Arrays.asList("省份",
                "城市",
                "所属区县",
                "所属乡镇",
                "所属村",
                "机构名称",
                "姓名",
                "性别",
                "民族",
                "身份证号码",
                "出生日期",
                "手机号",
                "住址",
                "签发机关",
                "有限起始日期",
                "有限结束日期"
               );

        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "人员信息导出", response);
        } catch (Exception e) {
        }
    }

    /**
     * 人员信息删除
     */
    @GetMapping("/delPersonInfo")
    public JsonObj delPersonInfo() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        return cerPersonInfoService.delPersonInfo(id);
    }
}
